package com.wgo.bpot.wiring;

import java.util.Date;

import com.wgo.bpot.common.transport.servicefacade.BpotServices;
import com.wgo.bpot.domain.common.Service;
import com.wgo.bpot.domain.common.UserSession;
import com.wgo.bpot.server.service.BpotServicesImpl;
import com.wgo.bpot.server.service.ModifyingOperationsState;

public class ServicesFactoryImpl implements ServicesFactory {
    private ModifyingOperationsState modifyingOperationsState;
    private ServiceAccessor serviceAccessor;
	
	public ServicesFactoryImpl() {
	}
	
    public ServicesFactoryImpl(ServiceAccessor serviceAccessor){
        this(serviceAccessor, new ModifyingOperationsState(new Date(),10000, serviceAccessor.getDomainModelConvention()));
    }
    
	public ServicesFactoryImpl(ServiceAccessor serviceAccessor,ModifyingOperationsState modifyingOperationsState) {
        this.serviceAccessor = serviceAccessor;
        this.modifyingOperationsState = modifyingOperationsState;
	}

	public BpotServices createServices(UserSession userSession, Service service){
		return configureServices(new BpotServicesImpl(), userSession);
	}
	
	protected <S extends BpotServicesImpl> S configureServices(S services, UserSession userSession){
        services.setServiceAccessor(serviceAccessor);
        services.setModifyingOperationsState(modifyingOperationsState);
        services.setUserSession(userSession);
		return services;
	}

    public Class<? extends BpotServices> getServicesApi() {
        return BpotServices.class;
    }

}
